<%@page import="com.MD5"%>
<%@page import="javax.swing.text.Document"%>
<%@page import="com.DBtools"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
     
    
    <%-- <jsp:include page="../header.jsp"></jsp:include> --%>
    <!-- 这是一个学院教师用户管理 -->
<!DOCTYPE html>
<html>
<head>
   <%@ include file="../bootstrap.jsp"%>
	<meta charset="UTF-8">
	<title>教师用户管理</title>
	<link href="../Style.css" rel="stylesheet" type="text/css" />
</head>
<style>
 div{margin: 20px; }
</style>
<body>
<h3 align="left">教师用户管理</h3>
<hr>
<div >
<%
//1.接收教师账号修改信息
request.setCharacterEncoding("UTF-8");
String UserNum=null;
UserNum=request.getParameter("TeaId");
String UserPwd=request.getParameter("NewPwd");

if(UserPwd !=null) UserPwd=new MD5().md5(UserPwd);
  System.out.println("加密："+UserPwd);

String pwd2=request.getParameter("ReNewPwd");
String btn=request.getParameter("btn");
String UserRole="教师";
String sql="";
String num="";
if(btn!=null && UserNum !=""){
	if (btn.equals("注册")){
	ResultSet rs2 = new DBtools().query("select * from user where UserNum="+UserNum);
	System.out.println("rs2"+rs2);
	while(rs2.next()){
		num = rs2.getString("UserNum");
		System.out.println("num"+rs2);
		}
	if(num.length()>0){ System.out.println("该账号已经注册！");
	}else{	
	sql = "insert into user values("+UserNum+",'"+UserPwd+"','"+UserRole+"')";
	int	r = new DBtools().execute(sql);
	}
}else{
	sql = "update user set UserPwd='"+UserPwd+"' where UserNum ='"+UserNum+"'";	
	int	r = new DBtools().execute(sql);
}

//out.println("sql="+sql+"<br>");	
}


//2.获取教师名单，分页显示

//连接数据库
Class.forName("org.mariadb.jdbc.Driver");
String url = "jdbc:mariadb://localhost:3306/epidemicmanagement";//地址+接口+数据库名
Connection conn = DriverManager.getConnection(url,"root","123456");
Statement stmt = conn.createStatement();


String depname=(String)request.getSession().getAttribute("UserName");
ResultSet rs = stmt.executeQuery("select * from teacher where DepName like '%"+depname+"%'");//'%"+kw+"%' 

//计算起止位置
rs.last();
int count=rs.getRow();

int end;
int items=8;

//获取当前页码
int pg=1;
if(count %items!=0 ){
	end=count/items+1;
}
else end=count/items;
/* System.out.println("总计数："+count);
out.println("<br>总计数："+count+"<br>"); */

String p=request.getParameter("p");
if(p!=null && Integer.parseInt(p)>0 && Integer.parseInt(p) <=end){
	pg=Integer.parseInt(p);
}

int start =(pg-1)*items;
int limit=pg*items<count ?pg*items :count;
if(start>0){
	 rs.absolute(start);
}else{
	 rs.beforeFirst();
}
System.out.println("区间:"+start+"-"+limit);

if(count %items!=0 ){
	end=count/items+1;
}
else end=count/items;

%>
<style> div{ margin:5px}</style>
<div>
<nav class="navbar navbar-light bg-light">
			<ul class="pagination">
				<!-- 显示上一页和下一页分页按钮 -->
				<li class="page-item"><a class="page-link" href="tealist.jsp?p=<%=1 %>">首页</a></li>			
				<li class="page-item "><a class="page-link" href="tealist.jsp?p=<%=pg-1%>">上一页</a></li>
				
				<li class="page-item"><a class="page-link" href="tealist.jsp?p=<%=pg+1 %>">下一页</a></li>
				
				<li class="page-item"><a class="page-link"  href="tealist.jsp?p=<%=end %>">尾页</a></li>
                <li class="page-link"><% out.println("第"+pg+"页/"+end+"页，共"+count+"条记录！");%></li>
			</ul>
			<a style="left: " href="teachdit.jsp?bol=<%="0"%>">新增教师</a>
			<!-- <a align="right" href="#">在此新增学院账号</a> -->
		</nav>


<table  class="table table-striped" >  <!-- imagetable -->
 <tr align="center">
            <th scope="col">教师工号</th>
            <th>教师姓名</th>
            <th hidden="hidden">账号密码</th> <!-- hidden="hidden" -->
            <th>性别</th>
            <th>邮箱</th>
            <th>年龄</th>
            <th>所属学院</th>
            <th colspan="3" width="70">操作</th>
        </tr>
    
<%while(rs.next()&&rs.getRow()<=limit ){
	String TeaId = rs.getString("TeaId");
%>
<tr onClick="rowclick(this)" align="center">	
	<td> <%=rs.getString("TeaId")%>  </td>
	<td> <%=rs.getString("TeaName")%>  </td>
	<td hidden="hidden" ><%ResultSet rs1 = new DBtools().query("select * from user where UserNum="+TeaId);
	while(rs1.next()){
		 out.print(rs1.getString("UserPwd"));}%></td>
	<td> <%=rs.getString("TeaGender")%> </td>
	<td> <%=rs.getString("TeaEmail")%>  </td>
	<td> <%=rs.getString("TeaAge")%>  </td>
	
	<td><%=rs.getString("DepName")%><%//选择学院 %></td>
	<td align="center"><a href="../TeaDel?TeaId=<%=TeaId%>" onClick="return confirm('请确认是否删除当前记录？')">删除</a></td>
	<td align="center"><a href="../resetpwd?TeaId=<%=TeaId%>&btn=<%="重置密码"%>" onClick="return confirm('请确认是否重置密码？')">重置密码</a></td>
	<td align="center"><a href="teachdit.jsp?TeaId=<%=TeaId%>&bol=<%="1"%>">修改</a></td>
</tr>
<%}%>
</table>
</div>
<% 
stmt.close();
conn.close();
rs.close();
%>
<!-- 修改教师账号密码 -->

<!-- <br>
<form name="inputacc">
<table width="300" align="center" border="1" class="imagetable">
<tr >
     <th colspan="2" ">教师账号管理</th>
        
  </tr>
<tr>
<td with="100">工号</td>
<td><input type="text" name="TeaId" id="TeaId"  readonly="readonly"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="TeaName"  id="TeaName"></td>
</tr>
<tr>
<td>原密码</td>
<td><input type="password" name="UserPwd"  id="UserPwd" ></td>
</tr>
<tr>
<td>新密码</td>
<td><input type="password" name="NewPwd"  id="NewPwd">
</td>
</tr>
<tr>
<td>请再次输入</td>
<td><input type="password" name="ReNewPwd"  id="ReNewPwd">
</td>
</tr>
<tr>
<td colspan="2"><text name="tx" id="tx"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="btn"  value="注册" id="btn1" onclick="return check1(this.form)" >
<input type="submit" name="btn" value="修改" id="btn2" onclick="return check2(this.form)" ></td>

</tr>
 </table>
 </form> -->
</body>
<script type="text/javascript">

var pwd="";
function rowclick(row){
	console.info(row);
	console.info(row.tagName); 
	TeaId.value=row.children[0].innerText;
	TeaName.value=row.children[1].innerText;
	tt=row.children[0].innerText;;
	pwd=row.children[2].innerText;
		if(row.children[2].innerText==""){
			UserPwd.value=null;
			/* UserPwd.value="该教师未注册账号！"; */
			 /* alert("该教师账号尚未注册账号，请先注册！"); */
		}else{
			UserPwd.value=row.children[2].innerText;
			
		}
		
    
}
   
   function check1(form){
	   /* var TeaId= document.getElementById("TeaId").value; */
	   /*  var btn1= document.getElementById("btn1").value; */
	   /*  var btn2= document.getElementById("btn2").value; */
	  /*  alert(btn1); */
	  // alert(pwd);
	   var ReNewPwd =document.getElementById("ReNewPwd").value;
       var NewPwd= document.getElementById("NewPwd").value;
	   var btn1=document.getElementById("btn1").value;
	   //alert(btn1);
	   
	   	if(document.inputacc.TeaId.value.length <=0){
		   alert("请输入账号");
		   document.inputacc.TeaId.focus();
		   return false;
		   }
	   	else if(pwd.length >0){   
	   	 	alert("该教师账号已注册！");
		   	return false;
	   	}
	   	else if(ReNewPwd.length<6 ||NewPwd !==ReNewPwd){
				   alert("密码不合法或不一致");
				   document.inputacc.ReNewPwd.focus();
				   document.inputacc.NewPwd.focus();
				   return false;
			   		} 
			   
	   	else{ 
	   		alert("注册成功！");
	   		return true;
	   	}
	
	   
   	}
   function check2(form){
	   /* var TeaId= document.getElementById("TeaId").value; */
	   /*  var btn1= document.getElementById("btn1").value; */
	   /*  var btn2= document.getElementById("btn2").value; */
	  /*  alert(btn1); */
	   //alert(pwd);
	   
	  
	   var ReNewPwd =document.getElementById("ReNewPwd").value;
       var NewPwd= document.getElementById("NewPwd").value;
	   var btn2=document.inputacc.btn2.value;
	  // alert(btn2);
	   
	   	if(document.inputacc.TeaId.value.length <=0){
		   alert("请输入账号");
		   document.inputacc.TeaId.focus();
		   return false;
		   }
	   	else if(ReNewPwd.length<6 ||NewPwd !==ReNewPwd){
				   alert("密码不合法或不一致");
				   document.inputacc.ReNewPwd.focus();
				   document.inputacc.NewPwd.focus();
				   return false;
			   		} 
			   
	   	else if(pwd.length <=0){   
	   	 	alert("该教师账号尚未注册，请先注册！");
		   	return false;
	   	}
	   	else {
	   	 	alert("修改成功！");
	   		return true;
	   	}
	
	   
   	}

   
 

</script>
</html>







